$(function () {
    InitTable();
    //根据窗口调整表格高度
    $(window).resize(function() {

        $('#carLogTab').bootstrapTable('resetView', {
            height: tableHeight()
        })
    });
    $("#searchPartsBtn").click(function () {
        InitTable();
    });
    function InitTable() {
        $('#partsTable').bootstrapTable('destroy');
        $('#partsTable').bootstrapTable({
            url: "/parts/selectAllParts",//数据源
            height: tableHeight(),//高度调整
            //search: true,//是否搜索
            pagination: true,//是否分页
            pageSize: 8,//单页记录数
            pageList: [5, 10, 20, 50],//分页步进值
            sidePagination: "client",//服务端分页
            contentType: "application/x-www-form-urlencoded",//请求数据内容格式 默认是 application/json 自己根据格式自行服务端处理
            dataType: "json",//期待返回数据类型
            method: "post",//请求方式
            //searchAlign: "right",//查询框对齐方式
            queryParamsType: "limit",//查询参数组织方式
            uniqueId: "cl_id",
            queryParams: function getParams(params) {
                return {
                    p_num:$("#p_searchNum").val(),
                    p_date:$("#p_searchDate").val(),
                    p_des:$("#p_searchDes").val(),

                }
            },
            searchOnEnterKey: false,//回车搜索
            //showRefresh: true,//刷新按钮
            //showColumns: true,//列选择按钮
            buttonsAlign: "right",//按钮对齐方式
            toolbar: "toolbar", //指定工具栏
            toolbarAlign: "right",//工具栏对齐方式
            columns: [
                {
                    title: "全选",
                    field: "select",
                    checkbox: true,
                    width: 20,//宽度
                    align: "center",//水平
                    valign: "middle"//垂直
                },
                {
                    field: "p_id",//键名
                    title: "id",//标题
                    sortable: true,//是否可排序
                    width:30,
                    order: "desc"//默认排序方式
                },
                {
                    field: "p_date",
                    title: "出库时间",
                    algin:"center",
                    width:300,
                    sortable: true
                },
                {
                    field: "p_num",
                    title: "出库数量",
                    sortable: true
                },
                {
                    field: "p_des",
                    title: "出库备注",
                    width:500,
                    align:"center",
                    sortable: true
                },
            ],
            onClickRow: function (row, $element) {
                //$element是当前tr的jquery对象
                $element.css("background-color", "#cecece");
            },//单击row事件
            locale: "zh-CN", //中文支持
            detailView: false, //是否显示详情折叠
            /*detailFormatter: function(index, row, element) {
                var html = '';
                $.each(row, function(key, val){
                    html += "<p>" + key + ":" + val +  "</p>"
                });
                return html;
            }*/
        });
    }
    function tableHeight() {
        return $(window).height() - 50;
    }
    $("#p_date").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });

    $("#p_searchDate").datetimepicker({
        format:'yyyy-mm-dd hh:ii',
        local:moment.locale('zh-cn'),
        startDate:"2000-01-01 00:00",
        endDate:"2050-01-01 00:00"

    });
    $("#addPartsBtn").click(function () {
        var p_id = $("#p_id").val();
        if (p_id ===""){
            $("#myDialogHeaderLable").text("添加记录");
            $("#addRecord").text("添加");
        }
    });
    $("#updatePartsBtn").click(function () {
        $("#addModal").modal("show");
        $("#myDialogHeaderLable").text("更新记录");
        $("#addRecord").text("更新");
        var table = $("#partsTable").bootstrapTable("getSelections");
        if(table !=undefined && table.length === 1){
            var result = table[0];
            $("#p_num").val(result.p_num),
            $("#p_date").val(result.p_date),
            $("#p_des").val(result.p_des)
        }
    });
    $("#deletePartsBtn").click(function () {
        var selected = $("#partsTable").bootstrapTable("getSelections");
        var ids = [];
        var len = selected.length;
        $("#deleteDialogMsg").html("你确定要删除"+len+"条记录吗");
    });
    $("#addRecord").click(function () {
        var cl_id = $("#p_id").val();
        if (cl_id===""){
            $.ajax({
                url:"/parts/insertParts",
                type:"post",
                dataType:"json",
                data:{
                    p_num:$("#p_num").val(),
                    p_date:$("#p_date").val(),
                    p_des:$("#p_des").val(),
                },
                success:function (result) {
                    var error = result.errorCode;
                    if (error == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#partsTable").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("插入失败")
                    }
                }
            })
        }else {
            $.ajax({
                url:"/parts/updatesParts",
                type:"post",
                dataType:"json",
                data: {
                    p_id:$("#p_id").val(),
                    p_num:$("#p_num").val(),
                    p_date: $("#p_date").val(),
                    p_des: $("#p_des").val(),
                },
                success:function (result) {
                    if (result.errorCode == 0){
                        toastr.success(result.status);
                        $("#addModal").modal("hide");
                        $("#partsTable").bootstrapTable("refresh");
                        clearModelData();
                    }else {
                        toastr.error("更新失败")
                    }
                }
            })

        }
    });
    $("#deleteBtn").click(function () {
        var selected = $("#partsTable").bootstrapTable("getSelections");
        var ids = [];
        var len = selected.length;
        if (len > 0){
            for (var i=0;i<len;i++){
                ids [i] = selected[i].p_id;
            }

            $.ajax({
                url:"/parts/deleteParts",
                type:"post",
                dataType:"json",
                //contentType:"application/json",
                // 序列化能够传传数组
                traditional: true,
                data:{
                    p_id: ids
                },
                success: function (result) {
                    var error = result.errorCode;
                    if (error == 0){
                        $("#deleteModal").modal('hide');
                        toastr.success(result.status);
                        $("#partsTable").bootstrapTable("refresh");
                    }
                }
            })
        }else {
            alert("至少选择一项");
            $("#deleteModal").modal('hide');
        }
    });
    function clearModelData() {
        $("#p_id").val("");
        $("#p_num").val(""),
        $("#p_date").val(""),
        $("#p_des").val("")
    }
});
